{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy.orm import sessionmaker\n",
    "\n",
    "engine = create_engine('sqlite:///:memory:')\n",
    "\n",
    "Session = sessionmaker(bind=engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "from sqlalchemy import Column, Integer, Numeric, String, Table, ForeignKey\n",
    "from sqlalchemy.orm import relationship\n",
    "from sqlalchemy.ext.declarative import declarative_base\n",
    "\n",
    "Base = declarative_base()\n",
    "\n",
    "\n",
    "cookieingredients_table = Table('cookieingredients', Base.metadata,\n",
    "    Column('cookie_id', Integer, ForeignKey(\"cookies.cookie_id\"),\n",
    "           primary_key=True),\n",
    "    Column('ingredient_id', Integer, ForeignKey(\"ingredients.ingredient_id\"),\n",
    "           primary_key=True)\n",
    ")\n",
    "\n",
    "\n",
    "class Ingredient(Base):\n",
    "    __tablename__ = 'ingredients'\n",
    "    \n",
    "    ingredient_id = Column(Integer, primary_key=True)\n",
    "    name = Column(String(255), index=True)\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return \"Ingredient(name='{self.name}')\".format(self=self)\n",
    "\n",
    "\n",
    "class Cookie(Base):\n",
    "    __tablename__ = 'cookies'\n",
    "\n",
    "    cookie_id = Column(Integer, primary_key=True)\n",
    "    cookie_name = Column(String(50), index=True)\n",
    "    cookie_recipe_url = Column(String(255))\n",
    "    cookie_sku = Column(String(55))\n",
    "    quantity = Column(Integer())\n",
    "    unit_cost = Column(Numeric(12, 2))\n",
    "    \n",
    "    ingredients = relationship(\"Ingredient\", secondary=cookieingredients_table)\n",
    "        \n",
    "    def __repr__(self):\n",
    "        return \"Cookie(cookie_name='{self.cookie_name}', \" \\\n",
    "                       \"cookie_recipe_url='{self.cookie_recipe_url}', \" \\\n",
    "                       \"cookie_sku='{self.cookie_sku}', \" \\\n",
    "                       \"quantity={self.quantity}, \" \\\n",
    "                       \"unit_cost={self.unit_cost})\".format(self=self)\n",
    " \n",
    "\n",
    "Base.metadata.create_all(engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "session = Session()\n",
    "cc_cookie = Cookie(cookie_name='chocolate chip', \n",
    "                   cookie_recipe_url='http://some.aweso.me/cookie/recipe.html', \n",
    "                   cookie_sku='CC01', \n",
    "                   quantity=12, \n",
    "                   unit_cost=0.50)\n",
    "flour = Ingredient(name='Flour')\n",
    "sugar = Ingredient(name='Sugar')\n",
    "egg = Ingredient(name='Egg')\n",
    "cc = Ingredient(name='Chocolate Chips')\n",
    "cc_cookie.ingredients.extend([flour, sugar, egg, cc])\n",
    "session.add(cc_cookie)\n",
    "session.flush()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Flour', 'Sugar', 'Egg', 'Chocolate Chips']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[ingredient.name for ingredient in cc_cookie.ingredients]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
